<?php

/**
 * This is the model class for table "ndk_health".
 *
 * The followings are the available columns in table 'ndk_health':
 * @property integer $id
 * @property string $nota_debet_no
 * @property string $policy_no
 * @property string $endorsement_no
 * @property double $nominal
 * @property string $premium
 * @property double $policy_cost
 * @property string $time_duty
 * @property double $admin_broker
 * @property double $discount
 * @property integer $discount_percent
 * @property double $adjusment
 * @property string $due_date
 * @property double $brokerage
 * @property integer $brokerage_percent
 * @property integer $ppn
 * @property integer $ppn_percent
 * @property integer $pph
 * @property integer $pph_percent
 * @property string $note
 * @property integer $createdby
 * @property string $createdtime
 * @property integer $updatedby
 * @property string $updatedtime
 */
class NdkHealth extends CActiveRecord
{
public $name;
	public $status;

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ndk_health';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('nota_debet_no, reg_health_policy_id', 'required'),
			array('discount_percent, brokerage_percent, ppn_percent, pph_percent, diskon_asuransi_percent, client_tahu_diskon, posting, stampduty_asuransi, value_brokerage, createdby, updatedby,installment_ke, premium, ndk_health_master_id', 'numerical', 'integerOnly'=>true),
			array('nominal, policy_cost, admin_broker, discount, adjusment, brokerage, nett_to_insurance, jml_bayar, kurang_bayar, sisa_bayar, diskon_asuransi, ppn, pph', 'numerical'),
			array('nota_debet_no, endorsement_no', 'length', 'max'=>50),
			array('type', 'length', 'max'=>5),
			array('note', 'length', 'max'=>100),
			array('is_bayar', 'length', 'max'=>1),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('ndk_health_master_id, type, value_brokerage, reg_health_policy_id, diskon_asuransi, diskon_asuransi_percent, client_tahu_diskon, posting, nett_to_insurance, is_bayar, jml_bayar, id, nota_debet_no, endorsement_no, nominal, premium, policy_cost, stamp_duty, admin_broker, discount, discount_percent, adjusment, due_date, brokerage, brokerage_percent, ppn, ppn_percent, pph, pph_percent, note, createdby, createdtime, updatedby, updatedtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nota_debet_no' => 'No Nota Debet',
			'endorsement_no' => 'Endorsement',
			'nominal' => 'Total',
			'premium' => 'Premi',
			'policy_cost' => 'Biaya Polis',
			//'stamp_duty' => 'Stamp Duty',
			'stamp_duty' => 'Materai Agra',
			'admin_broker' => 'Admin Broker',
			'discount' => 'Diskon Agra',
			'discount_percent' => 'Discount Percent',
			'adjusment' => 'Adjusment',
			'due_date' => 'Due Date',
			'brokerage' => 'Brokerage',
			'brokerage_percent' => 'Brokerage Percent',
			'ppn' => 'Ppn',
			'ppn_percent' => 'Ppn Percent',
			'pph' => 'Pph',
			'pph_percent' => 'Pph Percent',
			'note' => 'Note',
			'createdby' => 'Createdby',
			'createdtime' => 'Tanggal Input',
			'updatedby' => 'Updatedby',
			'updatedtime' => 'Updatedtime',
			'is_bayar' => 'Is Bayar',
			'nett_to_insurance' => 'Nett To Insurance',
			'diskon_asuransi' => 'Diskon Asuransi',
			'diskon_asuransi_percent' => 'Diskon Asuransi Percent',
			'client_tahu_diskon' => 'Client Tahu Diskon Asuransi',
			'posting' => 'Posting',
			'jml_bayar'=>'Jumlah Bayar',
			'reg_health_policy_id' => 'reg_health_policy_id',
			'type' => 'Class Of Business',		
			'value_brokerage' => 'value_brokerage',	
			'stampduty_asuransi' => 'Materai Asuransi',
			'sisa_bayar' => 'sisa_bayar',			
			'kurang_bayar' => 'kurang_bayar',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->select = 'distinct(reg_health_policy_id) as reg_health_policy_id,ndk_health_master_id, sum(nominal)as nominal, sum(premium)as premium, posting';
		$criteria->group = 'reg_health_policy_id, ndk_health_master_id, posting';

		$criteria->compare('id',$this->id);
		$criteria->compare('nota_debet_no',$this->nota_debet_no,true);
		$criteria->compare('endorsement_no',$this->endorsement_no,true);
		$criteria->compare('nominal',$this->nominal);
		$criteria->compare('premium',$this->premium,true);
		$criteria->compare('policy_cost',$this->policy_cost);
		$criteria->compare('stamp_duty',$this->stamp_duty,true);
		$criteria->compare('admin_broker',$this->admin_broker);
		$criteria->compare('discount',$this->discount);
		$criteria->compare('discount_percent',$this->discount_percent);
		$criteria->compare('adjusment',$this->adjusment);
		$criteria->compare('due_date',$this->due_date,true);
		$criteria->compare('brokerage',$this->brokerage);
		$criteria->compare('brokerage_percent',$this->brokerage_percent);
		$criteria->compare('ppn',$this->ppn);
		$criteria->compare('ppn_percent',$this->ppn_percent);
		$criteria->compare('pph',$this->pph);
		$criteria->compare('pph_percent',$this->pph_percent);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('createdby',$this->createdby);
		$criteria->compare('createdtime',$this->createdtime,true);
		$criteria->compare('updatedby',$this->updatedby);
		$criteria->compare('updatedtime',$this->updatedtime,true);
		$criteria->compare('nett_to_insurance',$this->nett_to_insurance,true);
		$criteria->compare('jml_bayar',$this->jml_bayar,true);
		$criteria->compare('diskon_asuransi',$this->diskon_asuransi,true);
		$criteria->compare('diskon_asuransi_percent',$this->diskon_asuransi_percent,true);
		$criteria->compare('client_tahu_diskon',$this->client_tahu_diskon,true);
		$criteria->compare('posting',$this->posting,true);
		$criteria->compare('reg_health_policy_id',$this->reg_health_policy_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}



	public function searchView($id)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->condition = 't.reg_health_policy_id='.$id;

		$criteria->compare('id',$this->id);
		$criteria->compare('nota_debet_no',$this->nota_debet_no,true);
		$criteria->compare('endorsement_no',$this->endorsement_no,true);
		$criteria->compare('nominal',$this->nominal);
		$criteria->compare('premium',$this->premium,true);
		$criteria->compare('policy_cost',$this->policy_cost);
		$criteria->compare('stamp_duty',$this->stamp_duty,true);
		$criteria->compare('admin_broker',$this->admin_broker);
		$criteria->compare('discount',$this->discount);
		$criteria->compare('discount_percent',$this->discount_percent);
		$criteria->compare('adjusment',$this->adjusment);
		$criteria->compare('due_date',$this->due_date,true);
		$criteria->compare('brokerage',$this->brokerage);
		$criteria->compare('brokerage_percent',$this->brokerage_percent);
		$criteria->compare('ppn',$this->ppn);
		$criteria->compare('ppn_percent',$this->ppn_percent);
		$criteria->compare('pph',$this->pph);
		$criteria->compare('pph_percent',$this->pph_percent);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('createdby',$this->createdby);
		$criteria->compare('createdtime',$this->createdtime,true);
		$criteria->compare('updatedby',$this->updatedby);
		$criteria->compare('updatedtime',$this->updatedtime,true);
		$criteria->compare('nett_to_insurance',$this->nett_to_insurance,true);
		$criteria->compare('jml_bayar',$this->jml_bayar,true);
		$criteria->compare('diskon_asuransi',$this->diskon_asuransi,true);
		$criteria->compare('diskon_asuransi_percent',$this->diskon_asuransi_percent,true);
		$criteria->compare('client_tahu_diskon',$this->client_tahu_diskon,true);
		$criteria->compare('posting',$this->posting,true);
		$criteria->compare('reg_health_policy_id',$this->reg_health_policy_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	
	public function searchNota()	
	{
		// @todo Please modify the following code to remove attributes that should not be searched.
		$criteria=new CDbCriteria;
		$tes = $criteria->compare('nota_debet_no',$this->nota_debet_no,true);
		
		$sql=" SELECT a.* ,d.name
FROM  (
SELECT id,nota_debet_no,policy_no,'1' as status from ndk_health UNION SELECT id,nota_debet_no,policy_no,'2' as status from ndk_health_non
) a 
left join reg_health_policy b on a.policy_no = b.policy_no 
left join reg_health_policy_non c on a.policy_no = c.policy_no 
left join mst_client d on b.mst_client_id = d.id or c.mst_client_id = d.id
where 1=1
 ";
		if($this->nota_debet_no != ""){
			$sql .= " AND a.nota_debet_no like '".$this->nota_debet_no."%' ";
		} 
		
		$count = count(Yii::app()->db->createCommand($sql)->queryAll());
		$dataProvider=new CSqlDataProvider($sql, array(
			'totalItemCount'=>$count,
			/*'sort'=>array(
				'attributes'=>array(
					 'nota_debet_no', //and all other atributes with withc you want to sort
				),
			),*/
			'pagination'=>array(
				'pageSize'=>10,
			),
		));
		return $dataProvider;
	}
	
	

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return NdkHealth the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
